﻿

namespace LogManager.Utils.Diagnostics
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// A TraceListener that raise an event each time a trace is written
    /// </summary>
    public sealed class ObservableTraceListener : TraceListener
    {
        StringBuilder buffer = new StringBuilder();

        public override void Write(string message)
        {
            buffer.Append(message);
        }

        [DebuggerStepThrough]
        public override void WriteLine(string message)
        {
            buffer.Append(message);

            if (TraceCatched != null)
            {
                TraceCatched(buffer.ToString());
            }

            buffer.Clear();
        }

        public event Action<string> TraceCatched;
    }
}
